/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp.view.simulation;

import jade.core.AID;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.newdawn.slick.CanvasGameContainer;
import org.newdawn.slick.SlickException;

import ca.usherbrooke.agent.BasicAgent;
import ca.usherbrooke.behaviours.config.RequestSimulationIncSpeed;
import ca.usherbrooke.behaviours.config.RequestSimulationPause;
import ca.usherbrooke.behaviours.config.RequestSimulationStart;
import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.view.properties.ViewSlickProperties;
import ca.usherbrooke.view.slick.jade.IWorldProvider;
import ca.usherbrooke.view.slick.jade.WorldViewSlickJade;
import ca.usherbrooke.view.slick.jade.worldView.EntityWorldView;
import ca.usherbrooke.view.slick.jade.worldView.GlobalWorldView;

/**
 * @author Cody Stoutenburg
 * 
 */
public class PanelSimulation extends JPanel {
	private static final long serialVersionUID = 1L;

	private final CanvasGameContainer container;
	private final BasicAgent agent;
	private final AID worldAgent;

	private final WorldViewSlickJade game;

	private final HashMap<Long, JPanel> mapIdPanel;
	private final JPanel goalPanel;

	private class SelectViewListener implements ActionListener {
		private final Integer viewSelected;

		public SelectViewListener(Integer view) {
			viewSelected = view;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			game.setCurrentView(viewSelected);
		}

	}

	public PanelSimulation(BasicAgent agent, IWorldProvider provider,
			AID worldAgent, ViewSlickProperties property) throws SlickException {
		super(new BorderLayout());

		this.agent = agent;
		this.worldAgent = worldAgent;

		mapIdPanel = new HashMap<Long, JPanel>();

		JTabbedPane tabbedPane = new JTabbedPane();

		JPanel panelGlobalView = new JPanel(new BorderLayout());

		JPanel panelNorth = new JPanel(new FlowLayout());
		JButton buttonPlay = new JButton("Play");
		JButton buttonPause = new JButton("Pause");
		JButton buttonDecSpeed = new JButton("Dec Speed");
		JButton buttonIncSpeed = new JButton("Inc Speed");

		buttonPlay.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				play();
			}
		});

		buttonPause.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				pause();
			}
		});

		buttonDecSpeed.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				decSpeed();
			}
		});

		buttonIncSpeed.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				incSpeed();
			}
		});

		panelNorth.add(buttonPlay);
		panelNorth.add(buttonPause);
		panelNorth.add(buttonDecSpeed);
		panelNorth.add(buttonIncSpeed);

		JPanel panelWest = new JPanel();

		panelWest.setLayout(new BoxLayout(panelWest, BoxLayout.Y_AXIS));
		JButton buttonMainView = new JButton("Global World View");
		buttonMainView.addActionListener(new SelectViewListener(0));
		panelWest.add(buttonMainView);
		Integer i = 1;
		game = new WorldViewSlickJade("Simple View", property);
		game.addWorldView(new GlobalWorldView(provider));
		for (IEntityModel entity : provider.getWorld().getAllEntities()) {
			game.addWorldView(new EntityWorldView(provider, entity.getGuid()));
			JButton buttonEntityView = new JButton("View of "
					+ entity.getGuid());
			buttonEntityView.addActionListener(new SelectViewListener(i));
			panelWest.add(buttonEntityView);
			++i;
		}

		container = new CanvasGameContainer(game);
		container.setSize(800, 600);

		panelGlobalView.add(panelNorth, BorderLayout.NORTH);
		panelGlobalView.add(panelWest, BorderLayout.WEST);
		panelGlobalView.add(container, BorderLayout.CENTER);

		tabbedPane.addTab("Global View", panelGlobalView);

		goalPanel = new JPanel();
		tabbedPane.addTab("Goal Manager", goalPanel);

		IWorldModel world = provider.getWorld();
		List<IEntityModel> allEntities = world.getAllEntities();

		for (IEntityModel iEntityModel : allEntities) {
			Long guid = iEntityModel.getGuid();

			JPanel mainPanel = new JPanel();

			mapIdPanel.put(guid, mainPanel);
			tabbedPane.addTab(guid.toString(), mainPanel);
		}

		this.add(tabbedPane);
	}

	public JPanel getGoalPanel() {
		return goalPanel;
	}

	public JPanel getPanelForEntity(Long guid) {
		return this.mapIdPanel.get(guid);
	}

	public void start() throws SlickException {
		container.getContainer().setAlwaysRender(true);
		container.start();
	}

	public void stop() throws SlickException {
		container.getContainer().exit();
	}

	private void play() {
		// ask the world to play
		this.agent.addBehaviour(new RequestSimulationStart(agent, worldAgent));
	}

	private void pause() {
		// ask the world to pause
		this.agent.addBehaviour(new RequestSimulationPause(agent, worldAgent));
	}

	private void decSpeed() {
		// ask the world to dec speed
		this.agent.addBehaviour(new RequestSimulationIncSpeed(agent,
				worldAgent, -50));
	}

	private void incSpeed() {
		// ask the world to inc speed
		this.agent.addBehaviour(new RequestSimulationIncSpeed(agent,
				worldAgent, 50));
	}
}
